# -*- coding: utf-8 -*-
from odoo import models, fields, api
from datetime import datetime
import time
import json
import xmltodict

from odoo import http, exceptions
from odoo.http import request

import logging

_logger = logging.getLogger(__name__)

class Withrdrwaw(http.Controller):
    @http.route('/withdraw/money',auth='public', methods=['GET'], type='http')
    def getsomemoney(self, id=None, money=None,  **kwargs):
        _logger.info(id)
        _logger.info(money)
        userid = int(id)
        sum = float(money)

        Param = request.env()['ir.config_parameter']
        payable_1 = Param.get_param('payable_1') or ''
        payable_2 = Param.get_param('payable_2') or ''
        payable_1 = int(payable_1.encode("utf-8"))
        payable_2 = int(payable_2.encode("utf-8"))
        journal = request.env['account.journal'].sudo().search([('name','=',u'其它操作')])
        journal = journal[0]

        user = request.env['res.partner'].sudo().search([('id','=',userid)])
        # user = user[0]
        _logger.info(user.commission)
        if sum <= (user.commission - user.withdraw) and sum >= 1:
            payable = request.env['sale_sys.account_payable'].sudo().create({
                'user':user.id,
                'sum':sum,
                'apply_date': fields.Datetime.now(),
                'state':'draft'
            })

            move = request.env['account.move'].sudo().create({
                'journal_id': journal.id, #其他操作
                'date': datetime.today(),
                'name': 'INV' + str(datetime.today()),
                'state': 'posted',
            })
            label='/'
            move.line_ids.sudo().create({
            'move_id': move.id,
            'account_id': payable_1,      
            'debit': sum,      #借方金额
            'credit': 0.0,
            'date_maturity': datetime.today(),
            'partner_id': user.id,
            'name': label,
            })
            move.line_ids.sudo().create({
            'move_id': move.id,
            'account_id': payable_2,   
            'debit': 0.0,      
            'credit': sum,
            'date_maturity': datetime.today(),
            'partner_id': user.id,
            'name': label,
            })
            payable.voucher = move.id
            response = request.make_response(
                            headers={
                                "Content-Type": "json"
                            },
                            data=json.dumps({
                                'code':0,
                                'name': '卡哇伊',
                            })
                        )
        else:
            response = request.make_response(
                            headers={
                                "Content-Type": "json"
                            },
                            data=json.dumps({
                                'code':200,
                                'name': 'fail',
                            })
                        )
        return response